//
//  HomeCollectionViewCell.m
//  news
//
//  Created by imacbookair on 16/3/11.
//  Copyright © 2016年 imacbookair. All rights reserved.
//

#import "HomeCollectionViewCell.h"
#import "HomeTableViewCell.h"
#import "HomeAnotherTableViewCell.h"
#import <MJRefresh.h>
#import "AFNetBlock.h"
#import "HomeModel.h"
#import "LunBo.h"


@interface HomeCollectionViewCell ()<UITableViewDelegate, UITableViewDataSource>
@end

@implementation HomeCollectionViewCell


#pragma mark - 初始化

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.toutiaoTableView = [[UITableView alloc]initWithFrame:self.contentView.bounds style:UITableViewStylePlain];
        [self headerData];
        [self.contentView addSubview:_toutiaoTableView];
        [_toutiaoTableView NightWithType:UIViewColorTypeNormal];
    }
    return self;
}

#pragma mark - 布局
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
    _toutiaoTableView.delegate = self;
    _toutiaoTableView.dataSource = self;
    // tableView的右滚动条隐藏;
    
    _toutiaoTableView.showsVerticalScrollIndicator = NO;
    _toutiaoTableView.contentInset = UIEdgeInsetsMake(0, 0, 155 * KWITH, 0);
    //创建tableView的头部 用于加轮播图
    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 240 * KHIGHT)];
    header.backgroundColor = [UIColor whiteColor];
    _toutiaoTableView.tableHeaderView = header;
    
    // 重用池创建
    [_toutiaoTableView registerClass:[HomeTableViewCell class] forCellReuseIdentifier:@"cell"];
    [_toutiaoTableView registerClass:[HomeAnotherTableViewCell class] forCellReuseIdentifier:@"anotherCell"];
    
    MJRefreshNormalHeader *head = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self headerData];
            [self.toutiaoTableView.mj_header endRefreshing];
        });
    }];
    
    self.toutiaoTableView.mj_header = head;
    
    MJRefreshAutoFooter *foot = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [self footerData];
        [self.toutiaoTableView.mj_footer endRefreshing];
        
    }];
    self.toutiaoTableView.mj_footer = foot;
    
    
    
}

- (void) headerData
{
    NSString *str = @"http://api.iclient.ifeng.com/ClientNews?id=SYLB10,SYDT10,SYRECOMMEND&page=1&newShowType=1&province=%E8%BE%BD%E5%AE%81%E7%9C%81&city=%E5%A4%A7%E8%BF%9E%E5%B8%82&district=%E6%B2%99%E6%B2%B3%E5%8F%A3%E5%8C%BA&gv=4.5.1&av=4.5.1&uid=357507050009649&deviceid=357507050009649&proid=ifengnews&os=android_18&df=androidphone&vt=5&screen=1080x1920&publishid=2006";
    [AFNetBlock GetDataWithURL:str block:^(id block) {
        _dataOneArr = [HomeModel modelHanderWithArray:block[0][@"item"]];
        _dataTwoArr = [HomeModel modelHanderWithArray:block[0][@"slides"]];
        
        _touArr = block[1][@"item"];
        NSMutableArray *arr = [NSMutableArray array];
        for (NSInteger i = 0; i < _touArr.count; i++) {
            [arr addObject:_touArr[i][@"thumbnail"]];
        }
        LunBo *lunbo = [[LunBo alloc]initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, 240 * KHIGHT) URLArr:arr timeInterval:2 pageFrame:CGRectMake(280 * KWITH, 220 * KHIGHT, 80 * KWITH, 20 * KHIGHT) arr:nil];
        [_toutiaoTableView.tableHeaderView addSubview:lunbo];
        
        // 每四个普通cell后加一个三张图片的cell
        if (_dataOneArr.count > 15) {
            self.dataArr = [NSMutableArray arrayWithArray:_dataOneArr];
            for (NSInteger i = 0; i < _dataTwoArr.count; i++) {
                [_dataArr insertObject:_dataTwoArr[i] atIndex:((i + 1) * 4 + i)];
                
            }
            [_toutiaoTableView reloadData];
        }else{
            self.dataArr = [NSMutableArray arrayWithArray:_dataOneArr];
            for (NSInteger i = 0; i < _dataTwoArr.count - 1; i++) {
                [_dataArr insertObject:_dataTwoArr[i] atIndex:((i + 1) * 4 + i)];
                                                               
            }
            [_dataArr addObject:_dataTwoArr.lastObject];
        }
    }];
    [_toutiaoTableView reloadData];
}

- (void)footerData
{
    static NSInteger t = 2;
    NSString *str = @"http://api.iclient.ifeng.com/ClientNews?id=SYLB10,SYDT10,SYRECOMMEND&page=1&newShowType=1&province=%E8%BE%BD%E5%AE%81%E7%9C%81&city=%E5%A4%A7%E8%BF%9E%E5%B8%82&district=%E6%B2%99%E6%B2%B3%E5%8F%A3%E5%8C%BA&gv=4.5.1&av=4.5.1&uid=357507050009649&deviceid=357507050009649&proid=ifengnews&os=android_18&df=androidphone&vt=5&screen=1080x1920&publishid=2006";
    NSString *urlStr = [str stringByReplacingOccurrencesOfString:@"page=1" withString:[NSString stringWithFormat:@"page=%ld", t]];

    [AFNetBlock GetDataWithURL:urlStr block:^(id block) {
        NSArray *tempArr = [HomeModel modelHanderWithArray:block[0][@"item"]];
        for (NSInteger i = 0; i < tempArr.count; i++) {
            [_dataArr addObject:tempArr[i]];
        }
        [_toutiaoTableView reloadData];
    }];
    t += 1;
}

#pragma mark - tableView的协议实现
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeModel *m = _dataArr[indexPath.row];
    NSArray *arr = m.images;
    
    // 判断是不是位置  每过4个普通cell 加一个三图的cell
    if (arr.count == 0) {
        HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        [cell.tu sd_setImageWithURL:[NSURL URLWithString:m.thumbnail] zhanweitu];
        
        [cell NightWithType:UIViewColorTypeNormal];
        cell.title.text = m.title;
        
        cell.time.text = [m.updateTime substringWithRange:NSMakeRange(11, 5)];
        
        if (m.commentsall == nil) {
            cell.pin.text = @"0";
        }else{
            cell.pin.text = [NSString stringWithFormat:@"%@跟帖", m.commentsall];
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        HomeAnotherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"anotherCell" forIndexPath:indexPath];
        
        cell.title.text = m.title;
        
        [cell.zuo sd_setImageWithURL:[NSURL URLWithString:arr[0]]zhanweitu];
        [cell.zhong sd_setImageWithURL:[NSURL URLWithString:arr[1]]zhanweitu];
        [cell.you sd_setImageWithURL:[NSURL URLWithString:arr[2]]zhanweitu];
        
        cell.time.text = [m.updateTime substringWithRange:NSMakeRange(11, 5)];
        
        cell.pin.text = [NSString stringWithFormat:@"%@跟帖", m.commentsall];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell NightWithType:UIViewColorTypeNormal];
        return cell;
    }
}
#pragma mark - 设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeModel *m = _dataArr[indexPath.row];
    NSArray *arr = m.images;
    if (m.thumbnail.length == 0 && arr.count == 0) {
        return 0;
    }
    else if (arr.count == 0){
        return 110 * KHIGHT;
    }else{
        return 150 * KHIGHT;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeModel *m = _dataArr[indexPath.row];
//    if (indexPath.row == 0) {
//        [AFNetBlock GetDataWithURL:m.commentsUrl block:^(id block) {
//            
//        }];
//    }else{
        [self.delegate DadaValue:m.commentsUrl];
//    }
}
@end
